﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class BuildUploadCompletedWindow : EditorWindow
    {
        private float m_CopyLabelConfirmAlpha;
        private double m_CopyLabelConfirmStart;
        private string m_ErrorMessage = string.Empty;
        private bool m_RecoverableError;
        private string m_ShortURL = string.Empty;
        private bool m_Success;
        private string m_URL = string.Empty;
        private static Content s_Content;
        private static Styles s_Styles;

        private void CopyLabel(string content, string copyMessage)
        {
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            GUILayout.BeginHorizontal(options);
            GUILayout.Space(4f);
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            GUILayout.Label(new GUIContent((content.Length <= 0x21) ? content : (content.Substring(0, 0x21) + "...")), s_Styles.m_CopyLabelStyle, optionArray2);
            Rect lastRect = GUILayoutUtility.GetLastRect();
            GUILayout.Space(10f);
            GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.Width((float) s_Content.m_PasteboardIcon.image.width), GUILayout.Height((float) s_Content.m_PasteboardIcon.image.height) };
            GUILayout.Label(new GUIContent(s_Content.m_PasteboardIcon.image), GUIStyle.none, optionArray3);
            lastRect = new Rect(lastRect.x, lastRect.y, (lastRect.width + s_Content.m_PasteboardIcon.image.width) + 10f, Mathf.Max(lastRect.height, (float) s_Content.m_PasteboardIcon.image.height));
            switch (Event.current.type)
            {
                case EventType.MouseDown:
                    if (lastRect.Contains(Event.current.mousePosition))
                    {
                        GUIUtility.systemCopyBuffer = content;
                        this.m_CopyLabelConfirmStart = EditorApplication.timeSinceStartup;
                        this.m_CopyLabelConfirmAlpha = 1f;
                        Event.current.Use();
                    }
                    break;

                case EventType.Repaint:
                    if (lastRect.Contains(Event.current.mousePosition))
                    {
                        GUI.tooltip = s_Content.m_CopyToClipboardMessage.text;
                        Vector2 vector = GUIUtility.GUIToScreenPoint(new Vector2(lastRect.x, lastRect.y));
                        GUI.s_ToolTipRect = new Rect(vector.x, vector.y, lastRect.width, lastRect.height);
                    }
                    if ((this.m_CopyLabelConfirmAlpha > 0f) && (Event.current.type == EventType.Repaint))
                    {
                        lastRect = new Rect(lastRect.x, (lastRect.y + lastRect.height) - 1f, lastRect.width, lastRect.height);
                        Color color = GUI.color;
                        GUI.color = new Color(1f, 1f, 1f, this.m_CopyLabelConfirmAlpha);
                        EditorStyles.miniLabel.Draw(lastRect, new GUIContent(copyMessage), false, false, false, false);
                        GUI.color = color;
                    }
                    break;
            }
            GUILayout.EndHorizontal();
        }

        public static string GetShareURL(string url, string title, SocialNetwork network)
        {
            switch (network)
            {
                case SocialNetwork.LinkedIn:
                    return string.Format("http://www.linkedin.com/shareArticle?title={0}&source=%3A%2F%2Funity3d.com&url={1}", WWW.EscapeURL(title), WWW.EscapeURL(url));

                case SocialNetwork.Facebook:
                    return string.Format("http://www.facebook.com/sharer.php?t={0}&u={1}", WWW.EscapeURL(title), WWW.EscapeURL(url));

                case SocialNetwork.Twitter:
                    return string.Format("http://twitter.com/share?text={0}+{1}+%23unity3d", WWW.EscapeURL(title), WWW.EscapeURL(url));
            }
            throw new ArgumentException(string.Format("Social network {0} not supported", network));
        }

        private static BuildUploadCompletedWindow Launch()
        {
            BuildUploadCompletedWindow window = EditorWindow.GetWindow<BuildUploadCompletedWindow>(true);
            window.position = new Rect(100f, 100f, 470f, 170f);
            window.minSize = new Vector2(470f, 170f);
            window.maxSize = new Vector2(470f, 170f);
            return window;
        }

        private static BuildUploadCompletedWindow LaunchFailure(string message)
        {
            BuildUploadCompletedWindow window = Launch();
            window.title = s_Content.m_WindowTitleFailure.text;
            window.m_Success = false;
            window.m_ErrorMessage = message;
            return window;
        }

        public static void LaunchFailureCritical(string message)
        {
            LaunchFailure(message).m_RecoverableError = false;
        }

        public static void LaunchFailureRecoverable(string message)
        {
            LaunchFailure(message).m_RecoverableError = true;
        }

        public static void LaunchSuccess(string url)
        {
            BuildUploadCompletedWindow window = Launch();
            window.title = s_Content.m_WindowTitleSuccess.text;
            window.m_Success = true;
            window.m_URL = url;
            window.m_ShortURL = ShortenURL(url);
        }

        private bool LinkLabel(GUIContent content)
        {
            Rect lastRect;
            bool flag = !string.IsNullOrEmpty(content.text);
            if (flag)
            {
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
                GUILayout.Label(content, options);
                lastRect = GUILayoutUtility.GetLastRect();
            }
            else
            {
                lastRect = GUILayoutUtility.GetRect((float) content.image.width, (float) content.image.height);
            }
            EditorGUIUtility.AddCursorRect(lastRect, MouseCursor.Link);
            switch (Event.current.type)
            {
                case EventType.MouseDown:
                    if (lastRect.Contains(Event.current.mousePosition))
                    {
                        Event.current.Use();
                        return true;
                    }
                    break;

                case EventType.Repaint:
                    if (flag)
                    {
                        GUI.DrawTexture(new Rect(lastRect.x, (lastRect.y + lastRect.height) - 3f, lastRect.width, 1f), s_Styles.m_FontColorTexture);
                    }
                    else
                    {
                        GUI.DrawTexture(lastRect, content.image);
                    }
                    break;
            }
            return false;
        }

        private void OnFailureGUI()
        {
            GUILayout.Label(s_Content.m_TextHeaderFailure.text, EditorStyles.boldLabel, new GUILayoutOption[0]);
            if (this.m_RecoverableError)
            {
                GUILayout.Label(s_Content.m_MainTextFailureRecoverable.text + ":", EditorStyles.wordWrappedLabel, new GUILayoutOption[0]);
                GUILayout.Label(this.m_ErrorMessage, EditorStyles.wordWrappedLabel, new GUILayoutOption[0]);
            }
            else
            {
                GUILayout.Label(s_Content.m_MainTextFailureCritical.text + ":", EditorStyles.wordWrappedLabel, new GUILayoutOption[0]);
                GUILayout.Label(this.m_ErrorMessage, EditorStyles.wordWrappedLabel, new GUILayoutOption[0]);
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
                GUILayout.Label(s_Content.m_FailurePreLinkAssistText.text, options);
                if (this.LinkLabel(s_Content.m_UDNStatusLinkText))
                {
                    Application.OpenURL("http://forum.unity3d.com");
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(-8f);
                GUILayout.Label(s_Content.m_FailurePostLinkAssistText, new GUILayoutOption[0]);
            }
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            if (this.m_RecoverableError)
            {
                GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Width(120f), GUILayout.Height(20f) };
                if (GUILayout.Button(s_Content.m_CancelButton, optionArray2))
                {
                    base.Close();
                }
                GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.Width(120f), GUILayout.Height(20f) };
                if (GUILayout.Button(s_Content.m_FixButton, optionArray3))
                {
                    Application.OpenURL("http://forum.unity3d.com");
                    base.Close();
                }
            }
            else
            {
                GUILayoutOption[] optionArray4 = new GUILayoutOption[] { GUILayout.Width(120f), GUILayout.Height(20f) };
                if (GUILayout.Button(s_Content.m_CloseButton.text, optionArray4))
                {
                    base.Close();
                }
            }
            GUILayout.EndHorizontal();
        }

        private void OnGUI()
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }
            if (s_Content == null)
            {
                s_Content = new Content();
            }
            GUILayout.BeginArea(new Rect(5f, 5f, Screen.width - 10f, Screen.height - 10f));
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            Rect rect = GUILayoutUtility.GetRect((float) 150f, (float) 150f);
            if (Event.current.type == EventType.Repaint)
            {
                GUI.DrawTexture(new Rect(rect.x, rect.y, (float) s_Content.m_UDNLogo.image.width, (float) s_Content.m_UDNLogo.image.height), s_Content.m_UDNLogo.image);
            }
            GUILayout.BeginVertical(new GUILayoutOption[0]);
            if (this.m_Success)
            {
                this.OnSuccessGUI();
            }
            else
            {
                this.OnFailureGUI();
            }
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
        }

        private void OnSuccessGUI()
        {
            GUILayout.Label(s_Content.m_TextHeaderSuccess.text, EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUILayout.Label(s_Content.m_MainTextSuccess.text, EditorStyles.wordWrappedLabel, new GUILayoutOption[0]);
            GUILayout.Space(4f);
            this.CopyLabel(this.m_ShortURL, s_Content.m_DidCopyToClipboardMessage.text);
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            if (this.LinkLabel(s_Content.m_OpenInBrowser))
            {
                Application.OpenURL(this.m_URL);
            }
            GUILayout.Space(10f);
            if (this.LinkLabel(s_Content.m_Tweet))
            {
                Application.OpenURL(GetShareURL(this.m_ShortURL, s_Content.m_ShareMessage.text, SocialNetwork.Twitter));
            }
            GUILayout.Space(10f);
            if (this.LinkLabel(s_Content.m_LinkedInShare))
            {
                Application.OpenURL(GetShareURL(this.m_ShortURL, s_Content.m_ShareMessage.text, SocialNetwork.LinkedIn));
            }
            GUILayout.Space(10f);
            if (this.LinkLabel(s_Content.m_FacebookShare))
            {
                Application.OpenURL(GetShareURL(this.m_ShortURL, s_Content.m_ShareMessage.text, SocialNetwork.Facebook));
            }
            GUILayout.Space(5f);
            GUILayout.EndHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(120f), GUILayout.Height(20f) };
            if (GUILayout.Button(s_Content.m_CloseButton.text, options))
            {
                base.Close();
            }
            GUILayout.EndHorizontal();
        }

        public static string ShortenURL(string url)
        {
            return url;
        }

        private void Update()
        {
            if ((this.m_CopyLabelConfirmStart != 0.0) && ((EditorApplication.timeSinceStartup - this.m_CopyLabelConfirmStart) > 3.0))
            {
                this.m_CopyLabelConfirmAlpha = 1f - ((float) ((EditorApplication.timeSinceStartup - this.m_CopyLabelConfirmStart) - 3.0));
                if (this.m_CopyLabelConfirmAlpha <= 0f)
                {
                    this.m_CopyLabelConfirmAlpha = 0f;
                    this.m_CopyLabelConfirmStart = 0.0;
                }
                base.Repaint();
            }
        }

        private class Content
        {
            public const string kUDNPlayerControlPanelURL = "http://forum.unity3d.com";
            public const string kUDNStatusURL = "http://forum.unity3d.com";
            public GUIContent m_CancelButton = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.CancelButton");
            public GUIContent m_CloseButton = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.CloseButton");
            public GUIContent m_CopyToClipboardMessage = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.CopyToClipboardMessage");
            public GUIContent m_DidCopyToClipboardMessage = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.DidCopyToClipboardMessage");
            public GUIContent m_FacebookShare = EditorGUIUtility.IconContent("SocialNetworks.FacebookShare");
            public GUIContent m_FailurePostLinkAssistText = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.FailurePostLinkAssistText");
            public GUIContent m_FailurePreLinkAssistText = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.FailurePreLinkAssistText");
            public GUIContent m_FixButton = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.FixButton");
            public GUIContent m_LinkedInShare = EditorGUIUtility.IconContent("SocialNetworks.LinkedInShare");
            public GUIContent m_MainTextFailureCritical = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.MainTextFailureCritical");
            public GUIContent m_MainTextFailureRecoverable = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.MainTextFailureRecoverable");
            public GUIContent m_MainTextSuccess = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.MainTextSuccess");
            public GUIContent m_OpenInBrowser = EditorGUIUtility.IconContent("SocialNetworks.UDNOpen");
            public GUIContent m_PasteboardIcon = EditorGUIUtility.IconContent("Clipboard");
            public GUIContent m_ShareMessage = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.ShareMessage");
            public GUIContent m_TextHeaderFailure = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.TextHeaderFailure");
            public GUIContent m_TextHeaderSuccess = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.TextHeaderSuccess");
            public GUIContent m_Tweet = EditorGUIUtility.IconContent("SocialNetworks.Tweet");
            public GUIContent m_UDNLogo = EditorGUIUtility.IconContent("SocialNetworks.UDNLogo");
            public GUIContent m_UDNStatusLinkText = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.UDNStatusLinkText");
            public GUIContent m_WindowTitleFailure = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.WindowTitleFailure");
            public GUIContent m_WindowTitleSuccess = EditorGUIUtility.TextContent("BuildUploadCompletedWindow.WindowTitleSuccess");
        }

        public enum SocialNetwork
        {
            LinkedIn,
            Facebook,
            Twitter,
            GooglePlus,
            UnityDeveloperNetwork
        }

        private class Styles
        {
            public const float kBottomButtonHeight = 20f;
            public const float kBottomButtonWidth = 120f;
            public const float kCopyLabelConfirmDelay = 3f;
            public const float kCopyLabelIndentation = 4f;
            public const float kIconSpacing = 10f;
            public const float kIconSpacingRight = 5f;
            public const float kLogoAreaHeight = 150f;
            public const float kLogoAreaWidth = 150f;
            public const int kMaxCopyLabelLength = 0x21;
            public const float kTextPaddingBottom = 4f;
            public const float kWindowHeight = 170f;
            public const float kWindowPadding = 5f;
            public const float kWindowWidth = 470f;
            public const float kWindowX = 100f;
            public const float kWindowY = 100f;
            public GUIStyle m_CopyLabelStyle = new GUIStyle(EditorStyles.label);
            public Texture2D m_FontColorTexture;

            public Styles()
            {
                this.m_CopyLabelStyle.name = "CopyLabel";
                RectOffset offset = new RectOffset(0, 0, 0, 0);
                this.m_CopyLabelStyle.padding = offset;
                this.m_CopyLabelStyle.margin = offset;
                this.m_FontColorTexture = new Texture2D(1, 1);
                Color[] colors = new Color[] { EditorStyles.label.onNormal.textColor, EditorStyles.label.onNormal.textColor };
                this.m_FontColorTexture.SetPixels(colors);
                this.m_FontColorTexture.Apply();
            }
        }
    }
}

